import pygame, math, sys
from Ball import Ball
from Paddle import Paddle
pygame.init()

clock = pygame.time.Clock()

if pygame.mixer:
    pygame.mixer.init()

pygame.init()

# Most of this code taken from my previous balldemo game

screenWidth = 1200
screenHeight = 800

screenSize = screenWidth, screenHeight
screen = pygame.display.set_mode(screenSize)

ball1 = Ball("ball1.png", [5,8], screenSize)
ball2 = Ball("ball2.png", [4,6], screenSize)
ball2.place([300,200])
ball3 = Ball("ball3.png", [3,3], screenSize)
ball3.place([300,400])
ball4 = Ball("ball4.png", [3,7], screenSize)
ball4.place([500,400])
ball5 = Ball("ball5.png", [7,2], screenSize)
ball5.place([200,400])
ball6 = Ball("ball6.png", [6,4], screenSize)
ball6.place([300,500])
ball7 = Ball("ball7.png", [2,5], screenSize)
ball7.place([300,700])
ball8 = Ball("ball8.png", [9,9], screenSize)
ball8.place([500,700])
ball9 = Ball("ball9.png", [10,10], screenSize)
ball9.place([400,700])
paddle1 = Paddle("Paddle.png", [10,10], screenSize)
paddle1.place([100,500])


red = 0
green = 0
blue = 0
bgColor = red, green, blue

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP or event.key == pygame.K_w:
                paddle1.direction("up")
            elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
                paddle1.direction("down")
            elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                paddle1.direction("right")
            elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
                paddle1.direction("left")
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_UP or event.key == pygame.K_w:
                paddle1.direction("stop up")
            elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
                paddle1.direction("stop down")
            elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                paddle1.direction("stop right")
            elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
                paddle1.direction("stop left")
                
            
    ball1.move()
    ball1.wallCollide()
        
    ball2.move()
    ball2.wallCollide()
    
    ball3.move()
    ball3.wallCollide()
    
    ball4.move()
    ball4.wallCollide()
    
    ball5.move()
    ball5.wallCollide()
    
    ball6.move()
    ball6.wallCollide()
    
    ball7.move()
    ball7.wallCollide()
    
    ball8.move()
    ball8.wallCollide()
    
    ball9.move()
    ball9.wallCollide()
    
    paddle1.move()
    paddle1.wallCollide()
   
    
    if ball1.ballCollide(ball2):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball1.ballCollide(ball3):
        if red <255:
            red+=1
        else:
            red = 0
        if green <235:
            green+=20
        else:
            green = 0
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball2.ballCollide(ball3):
        if red <255:
            red+=1
        else:
            red = 0
        if green <255:
            green+=1
        else:
            green = 0
        if blue <235:
            blue+=20
        else:
            blue = 0
    if ball1.ballCollide(ball4):
        if red <250:
            red+=5
        else:
            red = 0
        if green <235:
            green+=20
        else:
            green = 0
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball2.ballCollide(ball4):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        else:
            green = 0
        if blue <250:
            blue+=5
        else:
            blue = 0
    if ball4.ballCollide(ball3):
        if red <255:
            red+=1
        else:
            red = 0
        if green <250:
            green+=5
        else:
            green = 0
        if blue <235:
            blue+=20
        else:
            blue = 0
    if ball1.ballCollide(ball5):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball5.ballCollide(ball3):
        if red <255:
            red+=1
        else:
            red = 0
        if green <235:
            green+=20
        else:
            green = 0
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball2.ballCollide(ball5):
        if red <255:
            red+=1
        else:
            red = 0
        if green <255:
            green+=1
        else:
            green = 0
        if blue <235:
            blue+=20
        else:
            blue = 0
    if ball5.ballCollide(ball4):
        if red <250:
            red+=5
        else:
            red = 0
        if green <235:
            green+=20
        else:
            green = 0
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball1.ballCollide(ball6):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball6.ballCollide(ball3):
        if red <255:
            red+=1
        else:
            red = 0
        if green <235:
            green+=20
        else:
            green = 0
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball2.ballCollide(ball6):
        if red <255:
            red+=1
        else:
            red = 0
        if green <255:
            green+=1
        else:
            green = 0
        if blue <235:
            blue+=20
        else:
            blue = 0    
    if ball6.ballCollide(ball4):
        if red <250:
            red+=5
        else:
            red = 0
        if green <235:
            green+=20
        else:
            green = 0
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball6.ballCollide(ball5):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball1.ballCollide(ball7):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball7.ballCollide(ball3):
        if red <255:
            red+=1
        else:
            red = 0
        if green <235:
            green+=20
        else:
            green = 0
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball2.ballCollide(ball7):
        if red <255:
            red+=1
        else:
            red = 0
        if green <255:
            green+=1
        else:
            green = 0
        if blue <235:
            blue+=20
        else:
            blue = 0
    if ball7.ballCollide(ball4):
        if red <250:
            red+=5
        else:
            red = 0
        if green <235:
            green+=20
        else:
            green = 0
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball7.ballCollide(ball5):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball7.ballCollide(ball6):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball1.ballCollide(ball8):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball8.ballCollide(ball2):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball8.ballCollide(ball3):
        if red <255:
            red+=1
        else:
            red = 0
        if green <235:
            green+=20
        else:
            green = 0
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball8.ballCollide(ball4):
        if red <250:
            red+=5
        else:
            red = 0
        if green <235:
            green+=20
        else:
            green = 0
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball8.ballCollide(ball5):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball8.ballCollide(ball6):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball7.ballCollide(ball8):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball1.ballCollide(ball9):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball9.ballCollide(ball3):
        if red <255:
            red+=1
        else:
            red = 0
        if green <255:
            green+=1
        else:
            green = 0
        if blue <235:
            blue+=20
        else:
            blue = 0
    if ball2.ballCollide(ball9):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        else:
            green = 0
        if blue <250:
            blue+=5
        else:
            blue = 0
    if ball9.ballCollide(ball4):
        if red <250:
            red+=5
        else:
            red = 0
        if green <235:
            green+=20
        else:
            green = 0
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball9.ballCollide(ball5):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball9.ballCollide(ball6):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball9.ballCollide(ball7):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if ball9.ballCollide(ball8):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if paddle1.ballCollide(ball2):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0   
    if paddle1.ballCollide(ball1):
        if red <255:
            red+=1
        else:
            red = 0
        if green <235:
            green+=20
        else:
            green = 0
        if blue <255:
            blue+=1
        else:
            blue = 0
    if paddle1.ballCollide(ball3):
        if red <255:
            red+=1
        else:
            red = 0
        if green <255:
            green+=1
        else:
            green = 0
        if blue <235:
            blue+=20
        else:
            blue = 0
    if paddle1.ballCollide(ball4):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        else:
            green = 0
        if blue <250:
            blue+=5
        else:
            blue = 0
    if paddle1.ballCollide(ball5):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if paddle1.ballCollide(ball6):
        if red <255:
            red+=1
        else:
            red = 0
        if green <255:
            green+=1
        else:
            green = 0
        if blue <235:
            blue+=20
        else:
            blue = 0  
    if paddle1.ballCollide(ball7):
        if red <255:
            red+=1
        else:
            red = 0
        if green <255:
            green+=1
        else:
            green = 0
        if blue <235:
            blue+=20
        else:
            blue = 0
    if paddle1.ballCollide(ball8):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        if blue <255:
            blue+=1
        else:
            blue = 0
    if paddle1.ballCollide(ball9):
        if red <235:
            red+=20
        else:
            red = 0
        if green <255:
            green+=1
        else:
            green = 0
        if blue <250:
            blue+=5
        else:
            blue = 0
    
    
    bgColor = red, green, blue

    screen.fill(bgColor)
    
    # Draw that Frame of the Ball!
    screen.blit(ball1.surface, ball1.rect)
    screen.blit(ball2.surface, ball2.rect)
    screen.blit(ball3.surface, ball3.rect)
    screen.blit(ball4.surface, ball4.rect)
    screen.blit(ball5.surface, ball5.rect)
    screen.blit(ball6.surface, ball6.rect)
    screen.blit(ball7.surface, ball7.rect)
    screen.blit(ball8.surface, ball8.rect)
    screen.blit(ball9.surface, ball9.rect)
    screen.blit(paddle1.surface, paddle1.rect)
    pygame.display.flip()